//
//  ForgotPasswordViewController.m
//  HuiKaoClient
//
//  Created by 龚 杰 on 13-3-27.
//  Copyright (c) 2013年 龚 杰. All rights reserved.
//

#import "ForgotPasswordViewController.h"
#import "MainTabBarController.h"
#import "AppDelegate.h"
@interface ForgotPasswordViewController ()

@end

@implementation ForgotPasswordViewController

- (id)init
{
    if (self = [super init]) {
        
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([m_titleBttBarItem superview] == nil) {
        [self.navigationController.navigationBar addSubview:m_titleBttBarItem];
    }
    if ([m_BackBttBarItem superview] == nil) {
        [self.navigationController.navigationBar addSubview:m_BackBttBarItem];
    }
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if ([m_BackBttBarItem superview]) {
        [m_BackBttBarItem removeFromSuperview];
    }
    if ([m_titleBttBarItem superview]) {
        [m_titleBttBarItem removeFromSuperview];
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    m_ForgotPasswordArray = [[NSMutableArray alloc] initWithObjects:@"邮 箱：",nil];
    
    
    m_titleBttBarItem = [[MainTabBarController setTitle:@"忘记密码"] retain];

    m_BackBttBarItem = [UIButton buttonWithType:UIButtonTypeCustom];
    m_BackBttBarItem.frame = CGRectMake(5, 7, 50, 30);
    [m_BackBttBarItem setBackgroundImage:[UIImage imageNamed:@"button_back.png"] forState:UIControlStateNormal];
    [m_BackBttBarItem setTitle:@"返回" forState:UIControlStateNormal];
    [m_BackBttBarItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    m_BackBttBarItem.titleLabel.font = [UIFont systemFontOfSize:13.0f];
    [m_BackBttBarItem addTarget:self action:@selector(changeBack) forControlEvents:UIControlEventTouchUpInside];
    [m_BackBttBarItem retain];
    
    m_IPTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height-44) style:UITableViewStyleGrouped];
    UIView* iview = [[UIView alloc] initWithFrame:m_IPTableView.frame];
    [iview setBackgroundColor:[UIColor colorWithRed:242/250.f green:242/255.0f blue:242/255.0f alpha:1.0]];
    m_IPTableView.backgroundView = iview;
    [iview release];
    [m_IPTableView setDataSource:self];
    [m_IPTableView setDelegate:self];
    [self.view addSubview:m_IPTableView];
    [self initFootView];
    
    m_InputField = [[UITextField alloc] initWithFrame:CGRectMake(80, 15, 250, 30)];
    m_InputField.returnKeyType =UIReturnKeyDone;
    m_InputField.delegate =self;
}

- (void)initFootView
{
    m_footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320,40)];
    UIButton* ibutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [ibutton setBackgroundImage:[UIImage imageNamed:@"button_yellow_l.png"] forState:UIControlStateNormal];
    ibutton.frame = CGRectMake(8, 0, 304,40);
    [ibutton setTitle:@"提交" forState:UIControlStateNormal];
    [ibutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [ibutton addTarget:self action:@selector(changeToSubmit) forControlEvents:UIControlEventTouchUpInside];
    [m_footView addSubview:ibutton];
    m_IPTableView.tableFooterView = m_footView;
}

- (void)changeBack
{
    self.hidesBottomBarWhenPushed = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)changeToSubmit
{
    [m_InputField resignFirstResponder];
    if (m_InputField.text.length>0) {
        HttpInterfaceManager* iHttpInterface = [HttpInterfaceManager sharedManager];
        iHttpInterface.delegate = self;
        [iHttpInterface ForgotPasswordRequestGet:m_InputField.text];
    }else{
        AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
        [tAppDel showWithCustomView:@"提示" detailText:@"请输入正确邮箱" isCue:YES delayTime:2.0f];
    }

}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [m_ForgotPasswordArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* tCellstr = @"EXRCell";
    UITableViewCell* tCell = [tableView dequeueReusableCellWithIdentifier:tCellstr];
    if (tCell == nil) {
        tCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tCellstr] autorelease];
        //        tCell.accessoryType = UITableViewCellAccessoryNone;
        tCell.selectionStyle = UITableViewCellSelectionStyleNone;
        UILabel* iName = [[UILabel alloc] initWithFrame:CGRectMake(5,0,70, 50)];
        iName.tag = 100;
        iName.backgroundColor = [UIColor clearColor];
        [iName setFont:[UIFont systemFontOfSize:16.0f]];
        [iName setTextColor:[UIColor colorWithRed:19.0/255.0f green:19.0/255.0f blue:19.0/255.0f alpha:1.0f]];
        iName.textAlignment = UITextAlignmentCenter;
        [tCell.contentView addSubview:iName];
        [iName release];
        
        if (indexPath.row == 0) {
            [tCell.contentView addSubview:m_InputField];
        }
    }
    
    UILabel* ilabel = (UILabel*)[tCell viewWithTag:100];
    ilabel.text = [m_ForgotPasswordArray objectAtIndex:indexPath.row];
    
    return tCell;
}


#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark-
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark-
#pragma mark HttpInterfaceManageDelegate
-(void)RequestResultsForgotPassword:(NSDictionary*)dic
{
    [self hudWasHidden:nil];
    if (dic != nil) {
        NSInteger result = ((NSString*)[dic objectForKey:@"result"]).integerValue;
        if (result == 1) {
            AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
            [tAppDel showWithCustomView:@"提示" detailText:@"密码已经发送到您的邮箱，请注意查收" isCue:YES delayTime:2.0f];
            [self performSelector:@selector(changeBack) withObject:nil afterDelay:2.5f];
        }else{
            AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
            [tAppDel showWithCustomView:@"提示" detailText:@"邮件发送失败" isCue:YES delayTime:2.0f];
        }
    }
}
-(void)RequestResultsErrer:(NSDictionary*)dic
{
    [self hudWasHidden:nil];
    AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [tAppDel showWithCustomView:@"提示" detailText:[dic objectForKey:@"error_msg"] isCue:YES delayTime:2.0f];
}

#pragma mark-
#pragma mark MBProgressHUDDelegate
- (void)hudWasHidden:(MBProgressHUD *)hud{
    m_MBHUD.delegate = nil;
	[m_MBHUD removeFromSuperview];
	[m_MBHUD release];
	m_MBHUD=nil;
}

-(void) addHudLoad{
	if (m_MBHUD == nil) {
		m_MBHUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
		// Add HUD to screen
        AppDelegate* tAppDel = (AppDelegate*)[UIApplication sharedApplication].delegate;
		[tAppDel.window addSubview:m_MBHUD];
		// Regisete for HUD callbacks so we can remove it from the window at the right time
		m_MBHUD.delegate = self;
		m_MBHUD.labelText = @"请求数据中...";
		[m_MBHUD show:YES];
	}
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dealloc
{
    [m_IPTableView release];
    [m_titleBttBarItem release];
    [m_BackBttBarItem release];
    [m_ForgotPasswordArray release];
    [m_footView release];
    [m_InputField release];

    [super dealloc];
}

@end
